﻿using System;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Routing;
using TSharp.Core.Mvc.Properties;

namespace TSharp.Core.Mvc.Web
{
    internal class TSharpControllerActivator : IControllerActivator
    {
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            try
            {
                return
                    (IController)
                    (DependencyResolver.Current.GetService(controllerType) ?? Activator.CreateInstance(controllerType));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    String.Format(
                        CultureInfo.CurrentCulture,
                        MvcResources.DefaultControllerFactory_ErrorCreatingController,
                        controllerType),
                    ex);
            }
        }
    }
}